package com.mdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserFriendService;
import com.mdd.admin.validate.UserFriendCreateValidate;
import com.mdd.admin.validate.UserFriendUpdateValidate;
import com.mdd.admin.validate.UserFriendSearchValidate;
import com.mdd.admin.vo.UserFriendListedVo;
import com.mdd.admin.vo.UserFriendDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserFriend;
import com.mdd.common.mapper.UserFriendMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 用户好友列实现类
 * @author LikeAdmin
 */
@Service
public class UserFriendServiceImpl implements IUserFriendService {
        
    @Resource
    UserFriendMapper userFriendMapper;

    /**
     * 用户好友列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserFriendListedVo>
     */
    @Override
    public PageResult<UserFriendListedVo> list(PageValidate pageValidate, UserFriendSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserFriend> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        userFriendMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:uid:long",
            "=:userId@user_id:long",
            "=:headPic@head_pic:str",
            "like:name:str",
            "=:phone:str",
            "=:birthday:str",
            "=:address:str",
            "=:position:str",
            "like:unitName@unit_name:str",
            "=:taxNumber@tax_number:str",
            "=:unitAddress@unit_address:str",
            "=:unitPhone@unit_phone:str",
            "like:bankName@bank_name:str",
            "=:bankCard@bank_card:str",
        });

        IPage<UserFriend> iPage = userFriendMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserFriendListedVo> list = new LinkedList<>();
        for(UserFriend item : iPage.getRecords()) {
            UserFriendListedVo vo = new UserFriendListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户好友列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return UserFriend
     */
    @Override
    public UserFriendDetailVo detail(Integer id) {
        UserFriend model = userFriendMapper.selectOne(
                new QueryWrapper<UserFriend>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserFriendDetailVo vo = new UserFriendDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户好友列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(UserFriendCreateValidate createValidate) {
        UserFriend model = new UserFriend();
        model.setUid(createValidate.getUid());
        model.setUserId(createValidate.getUserId());
        model.setHeadPic(createValidate.getHeadPic());
        model.setName(createValidate.getName());
        model.setPhone(createValidate.getPhone());
        model.setBirthday(createValidate.getBirthday());
        model.setAddress(createValidate.getAddress());
        model.setPosition(createValidate.getPosition());
        model.setUnitName(createValidate.getUnitName());
        model.setTaxNumber(createValidate.getTaxNumber());
        model.setUnitAddress(createValidate.getUnitAddress());
        model.setUnitPhone(createValidate.getUnitPhone());
        model.setBankName(createValidate.getBankName());
        model.setBankCard(createValidate.getBankCard());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userFriendMapper.insert(model);
    }

    /**
     * 用户好友列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserFriendUpdateValidate updateValidate) {
        UserFriend model = userFriendMapper.selectOne(
                new QueryWrapper<UserFriend>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUid(updateValidate.getUid());
        model.setUserId(updateValidate.getUserId());
        model.setHeadPic(updateValidate.getHeadPic());
        model.setName(updateValidate.getName());
        model.setPhone(updateValidate.getPhone());
        model.setBirthday(updateValidate.getBirthday());
        model.setAddress(updateValidate.getAddress());
        model.setPosition(updateValidate.getPosition());
        model.setUnitName(updateValidate.getUnitName());
        model.setTaxNumber(updateValidate.getTaxNumber());
        model.setUnitAddress(updateValidate.getUnitAddress());
        model.setUnitPhone(updateValidate.getUnitPhone());
        model.setBankName(updateValidate.getBankName());
        model.setBankCard(updateValidate.getBankCard());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userFriendMapper.updateById(model);
    }

    /**
     * 用户好友列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserFriend model = userFriendMapper.selectOne(
                new QueryWrapper<UserFriend>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userFriendMapper.delete(new QueryWrapper<UserFriend>().eq("id", id));
    }

}
